package com.grape.api;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageInfo;
import com.grape.annotation.OperateMethodName;
import com.grape.controller.BaseController;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import com.grape.entity.SysPermission;
import com.grape.service.ISysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;


/**
 * 权限表 控制器
 * @author  putao
 * @email   2628279194@qq.com
 */
@RestController
@RequestMapping("permissions")
@Api(tags = "权限接口")
@AllArgsConstructor
public class SysPermissionController extends BaseController<SysPermission> {

    private final ISysPermissionService sysPermissionService;

    @GetMapping
    @ApiOperation(value = "权限列表")
    @OperateMethodName(value = "权限表列表", url = "system:permissions:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<SysPermission> pageInfo = sysPermissionService.pageListByDelFlag(baseVo);
        return CollectionUtil.isNotEmpty(pageInfo.getList()) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }

    @PutMapping
    @ApiOperation(value = "权限修改")
    @OperateMethodName(value = "权限表修改", url = "system:permissions:update")
    public R<?> update(@Validated @RequestBody SysPermission sysPermission) {
        if (!Strings.isNotBlank(sysPermission.getId())) {
            return updateNotFound(sysPermission, null);
        }
        sysPermissionService.updateData(sysPermission);
        return updateSuccess(null, null);
    }


    @PostMapping
    @ApiOperation(value = "添加权限")
    @OperateMethodName(value = "添加权限", url = "system:permissions:insert")
    public R<?> insert(@Validated @RequestBody SysPermission sysPermission) {
        return insertSuccess(sysPermissionService.insertData(sysPermission), null);
    }

    @DeleteMapping
    @ApiOperation(value = "删除权限，可批量多个id逗号拼接")
    @OperateMethodName(value = "权限表删除,批量", url = "system:permissions:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空")  @RequestParam String ids) {
        int number = sysPermissionService.deleteData(ids);
        if (number < 0) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }

    @GetMapping("detail")
    @ApiOperation(value = "详情权限")
    @OperateMethodName(value = "权限表详情", url = "system:permissions:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        SysPermission sysPermission = sysPermissionService.detail(id);
        if (sysPermission != null) {
            return selectSuccess(sysPermission, id);
        }
        return selectNotFound(id, null);
    }

    @GetMapping("getByRoleId")
    @ApiOperation(value = "根据角色id查询角色权限和全部权限")
    @OperateMethodName(value = "根据角色id查询角色权限和全部权限", url = "system:permissions:getByRoleId")
    public R<?> getByRoleId(@NotBlank(message = "详情时id不能为空") String roleId) throws ExecutionException, InterruptedException {
        List<SysPermission> allPermissionList = new ArrayList<>();
        List<SysPermission> permissionList = sysPermissionService.selectByRoleId(roleId, allPermissionList);
        return selectSuccess(permissionList, allPermissionList);
    }
}

